package api

import (
	"FeedXmh/app/dao"
	jwt "github.com/gogf/gf-jwt"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
)

var Login_log = login_log{}

type login_log struct{}

func (*login_log) GET(r *ghttp.Request) {
	res := g.Map{
		"code":  0,
		"msg":   "",
		"count": 0,
		"data":  nil,
	}
	limit := r.GetInt("limit")
	page := r.GetInt("page")
	order := r.GetString("order")
	begin := r.GetString("begin")
	end := r.GetString("end")
	where := " 1=1 "
	if begin != "" {
		where += " and log_time>='" + begin + "' "
	}
	if end != "" {
		where += " and log_time<='" + end + "' "
	}

	claims := jwt.ExtractClaims(r)
	userid := claims["userid"].(string)
	url := r.URL.Path
	if dao.CheckApiPermission(url, r.Method, userid) {
		db := g.DB(g.Cfg().GetString("custom.db_name"))
		model := db.Model("v_login_log")
		count, err := model.Where(where).Count()
		if err != nil {
			g.Log().Line().Error(err)
		} else {
			res["count"] = count
			start := limit*page - limit
			if start < 0 {
				start = 0
			}
			if order != "" {
				model = model.Order(order)
			}
			list, error := model.Where(where).Limit(start, limit).All()
			if error != nil {
				g.Log().Line().Error(error)
			} else {
				res["data"] = list
			}
		}
	} else {
		res["code"] = -1
		res["msg"] = "Permission denied"
	}

	r.Response.WriteJson(res)
	r.ExitAll()
}
